<?php
include "func.php";
  echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<link rel="stylesheet" href="../style/default/style.css" type="text/css">
</head>
';
  
  if (!is_writable("../config.php")) {
echo "config.php is not writable"; die;
}
  
  echo "<table border=1><tr><th><img width='28px' height='28px' align='absmiddle' src='../style/default/img/brevisbb.png'>Welcome to <b>brevisBB</b></th></tr>";
  
  
 if ($_GET['step'] =="")
 {echo "<tr><td height='64px'><a class='menuButtons' title='click to begin install' href= 'install.php?step=1'>Install brevisBB</a></td></tr>";}


  if ($_GET['step'] ==1)
  {
  if ($_POST["server"]=="")
  {$s = "localhost";}
  if ($_POST["server"]!="")
  {$s = $_POST["server"];}
  echo "<form method='post' action=''>
  
  <td align='left'><span style='font-size:12px;'><br>Step <b>$_GET[step] / 3</b><br><br>Database Configuration:</span><br><br></td>
  </tr></table>
  <table style='width:100%' border=1>
  <tr><td align='right' height='26px'><span style='font-size:12px;'><b>Server</b> &nbsp; </span></td><td align='left'>
  <input value='$s' type='text' name='server' size='30' /></td></tr>
  
   <tr><td align='right' height='26px'><span style='font-size:12px;'><b>DB username</b> &nbsp; </span></td><td align='left'>
  <input value='$_POST[db_username]' type='text' name='db_username' size='30' /></td></tr>
  
   <tr><td align='right' height='26px'><span style='font-size:12px;'><b>DB password</b> &nbsp; </span></td><td align='left'>
  <input value='$_POST[db_password]' type='password' name='db_password' size='30' /></td></tr>
  
  <tr><td align='right' height='26px'><span style='font-size:12px;'><b>Database</b> &nbsp; </span></td><td align='left'>
  <input value='$_POST[db_database]' type='text' name='db_database' size='30' /></td></tr>
  
  <tr><td align='right' height='26px'><span style='font-size:12px;'><b>DB prefix</b>&nbsp; </span></td><td align='left'>
  <input value='$_POST[db_prefix]' type='text' name='db_prefix' size='30' /></td></tr>
  
  <tr><td></td>
    <td align='left'><br><input type='submit' class='inputButton' value='Test conection' /></td></tr>
  </table>
";

  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
  session_start();
  $s = $_POST["server"];
  $u = $_POST["db_username"];
  $p = $_POST["db_password"];
  $d = $_POST["db_database"];
  $conn = mysql_connect($s,$u,$p);
  if(!$conn) die('<table border=1><tr><td><br><span style="font-size:12px;color:red;">Error: could not establish database connection!</span><br><br></td></tr></table>');
  
  $status = mysql_select_db($d, $conn);
  if(!$status) die('<table border=1><tr><td><br><span style="font-size:12px;color:red;">Error: could not select the database!</span><br><br></td></tr></table>');
  else
  {
  echo '<table border=1><tr><td><span style="font-size:12px;color:green;">Connected to database!
  <br><br><a class="menuButtons" href="install.php?step=2">Continue</a></span><br><br></td></tr></table>';};
 
  $_SESSION['server'] 	= $_POST["server"];
  $_SESSION['db_username'] 	= $_POST["db_username"];
  $_SESSION['db_password'] 	= $_POST["db_password"];
  $_SESSION['db_database'] 	= $_POST["db_database"];
  $_SESSION['db_prefix'] 	= $_POST["db_prefix"];
  
  }
  
  } //END STEP 1
  
  
    if ($_GET['step'] ==2)
  {
  session_start();
  $con = mysql_connect($_SESSION['server'],$_SESSION['db_username'],$_SESSION['db_password']);
  mysql_select_db($_SESSION['db_database'], $con);
  echo "<form method='post' action=''>
  
  <td align='left'><span style='font-size:12px;'><br>Step <b>$_GET[step] / 3</b><br><br>Creating tables:</span><br><br></td>
  </tr>
  ";
//TABLE banlist
  $result1=mysql_query("CREATE TABLE $_SESSION[db_prefix]banlist(
  ban_id mediumint(8) NOT NULL auto_increment,
  ban_userid mediumint(8) NOT NULL,
  ban_ip varchar(40) character set utf8 collate utf8_bin NOT NULL,
  ban_email varchar(100) character set utf8 collate utf8_bin NOT NULL,
  ban_reason varchar(255) character set utf8 collate utf8_bin NOT NULL,
  ban_time timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (ban_id),
  KEY ban_user_id (ban_userid)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;")or die(mysql_error());
  if ($result1) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>banlist</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}
  
//TABLE categories
  $result2=mysql_query("CREATE TABLE $_SESSION[db_prefix]categories (
  cat_id mediumint(8) NOT NULL auto_increment,
  cat_name varchar(255) character set utf8 collate utf8_bin NOT NULL,
  cat_description varchar(255) character set utf8 collate utf8_bin NOT NULL,
  cat_posts mediumint(8) NOT NULL,
  cat_topics mediumint(8) NOT NULL,
  cat_last_post_id mediumint(8) NOT NULL,
  cat_last_poster_id mediumint(8) NOT NULL,
  cat_last_post_subject varchar(255) character set utf8 collate utf8_bin NOT NULL,
  cat_last_post_time datetime NOT NULL,
  cat_last_poster_name varchar(255) character set utf8 collate utf8_bin NOT NULL,
  cat_permission tinyint(2) NOT NULL,
  cat_icon varchar(40) character set utf8 collate utf8_bin NOT NULL,
  cat_parent mediumint(8) NOT NULL,
  cat_child mediumint(8) NOT NULL,
  cat_sort tinyint(2) NOT NULL,
  PRIMARY KEY  (cat_id),
  KEY cat_name_unique (cat_name)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;")or die(mysql_error());
  
  $result21 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'categories
  VALUES(1,"Category1","Category1 description",0,0,0,0,"","0000-00-00 00:00:00","",1,"forum_icon.png",0,0,0)') or die(mysql_error());
  
  $result22 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'categories
  VALUES(2,"Forum1","Forum1 description",1,1,1,1,"Welcome to brevissBB",NOW(),"",1,"forum_icon.png",1,0,1)') or die(mysql_error());

  if ($result2 AND $result21 AND $result22) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>categories</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}
  
//TABLE online_users
    $result3=mysql_query("CREATE TABLE $_SESSION[db_prefix]online_users (
  session char(32) character set utf8 collate utf8_bin NOT NULL,
  time int(11) NOT NULL,
  user varchar(40) character set utf8 collate utf8_bin NOT NULL,
  user_id int(8) NOT NULL,
  user_ip varchar(40) character set utf8 collate utf8_bin NOT NULL,
  PRIMARY KEY  (session),
  KEY user_id (user_id),
  KEY user_name (user)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;")or die(mysql_error());
  if ($result3) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>online_users</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}
  
//TABLE posts 
      $result4=mysql_query("CREATE TABLE  $_SESSION[db_prefix]posts (
  post_id mediumint(8) NOT NULL auto_increment,
  post_content mediumtext character set utf8 collate utf8_unicode_ci NOT NULL,
  post_date datetime NOT NULL,
  post_topic int(8) NOT NULL,
  post_cat mediumint(8) NOT NULL,
  post_by int(8) NOT NULL,
  post_edit_by varchar(255) character set utf8 collate utf8_bin NOT NULL,
  post_edit_date datetime NOT NULL,
  post_subject varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL,
  post_smilies int(4) NOT NULL,
  poster_ip varchar(40) character set utf8 collate utf8_bin NOT NULL,
  post_page mediumint(8) NOT NULL,
  post_approve tinyint(1) NOT NULL,
  PRIMARY KEY  (post_id),
  KEY post_topic (post_topic),
  KEY post_by (post_by)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")or die(mysql_error());

  $result41 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'posts
  VALUES(1,"Hello world,
\n<br>
\n<br>Welcome to <b>brevissBB</b>.",NOW(),1,2,1,"","0000-00-00 00:00:00","Welcome to brevissBB",0,"",0,0)') or die(mysql_error());
 
  if ($result4 and $result41) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>posts</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}
  
//TABLE topics
    $result5=mysql_query("CREATE TABLE  $_SESSION[db_prefix]topics (
  topic_id int(8) NOT NULL auto_increment,
  topic_subject varchar(255) character set utf8 collate utf8_bin NOT NULL,
  topic_date datetime NOT NULL,
  topic_cat int(8) NOT NULL,
  topic_by int(8) NOT NULL,
  view mediumint(8) NOT NULL default '0',
  reply mediumint(8) NOT NULL default '0',
  topic_starter varchar(255) character set utf8 collate utf8_bin NOT NULL,
  topic_last_poster_id mediumint(8) NOT NULL,
  topic_last_poster varchar(30) character set utf8 collate utf8_bin NOT NULL,
  topic_last_post_date datetime NOT NULL,
  topic_status tinyint(2) NOT NULL,
  topic_sort tinyint(2) NOT NULL,
  topic_icon varchar(30) character set utf8 collate utf8_bin NOT NULL,
  PRIMARY KEY  (topic_id),
  KEY topic_cat (topic_cat),
  KEY topic_by (topic_by)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;")or die(mysql_error());


$result51 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'topics VALUES(1,"Welcome to brevissBB",NOW(),2,1,1,0,"",1,"",NOW(),0,0,"normal.gif");') or die(mysql_error());

  if ($result5 and $result51) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>topics</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}  
   
//TABLE topics_track
    $result6=mysql_query("CREATE TABLE $_SESSION[db_prefix]topics_track (
  user_id mediumint(8) NOT NULL,
  topic_id mediumint(8) NOT NULL,
  forum_id mediumint(8) NOT NULL,
  mark_time datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;")or die(mysql_error()); 
  
    if ($result6) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>topics_track</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}  
  
//TABLE topics_watch
    $result7=mysql_query("CREATE TABLE $_SESSION[db_prefix]topics_watch (
  topic_id mediumint(8) NOT NULL,
  user_id mediumint(8) NOT NULL,
  status tinyint(1) NOT NULL,
  KEY topic_id (topic_id),
  KEY user_id (user_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;")or die(mysql_error()); 
  
    if ($result7) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>topics_watch</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}  
  
//TABLE users
    $result8=mysql_query("CREATE TABLE $_SESSION[db_prefix]users (
  user_id int(8) NOT NULL auto_increment,
  user_name varchar(30) character set utf8 collate utf8_bin NOT NULL,
  user_pass varchar(255) character set utf8 collate utf8_bin NOT NULL,
  user_email varchar(255) character set utf8 collate utf8_bin NOT NULL,
  user_date datetime NOT NULL,
  user_level tinyint(4) NOT NULL,
  user_posts mediumint(8) NOT NULL,
  user_last_login datetime NOT NULL,
  user_ip varchar(40) character set utf8 collate utf8_bin NOT NULL,
  user_avatar varchar(255) character set utf8 collate utf8_bin NOT NULL,
  confirm_code varchar(65) character set utf8 collate utf8_bin NOT NULL,
  activation_code varchar(65) character set utf8 collate utf8_bin NOT NULL,
  user_lang varchar(10) character set utf8 collate utf8_bin NOT NULL,
  user_signature varchar(255) character set utf8 collate utf8_bin NOT NULL,
  is_banned tinyint(1) NOT NULL,
  show_email tinyint(1) NOT NULL,
  session char(32) character set utf8 collate utf8_bin NOT NULL,
  user_msn varchar(40) character set utf8 collate utf8_bin NOT NULL,
  user_www varchar(200) character set utf8 collate utf8_bin NOT NULL,
  user_location varchar(100) character set utf8 collate utf8_bin NOT NULL,
  user_occupation text character set utf8 collate utf8_bin NOT NULL,
  user_interests text character set utf8 collate utf8_bin NOT NULL,
  user_birthday varchar(10) character set utf8 collate utf8_bin NOT NULL default ' 0-0-0',
  user_show_age tinyint(1) NOT NULL,
  user_gender tinyint(1) NOT NULL,
  user_about mediumtext character set utf8 collate utf8_bin NOT NULL,
  user_browser varchar(255) character set utf8 collate utf8_bin NOT NULL,
  PRIMARY KEY  (user_id),
  UNIQUE KEY user_name_unique (user_name)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;")or die(mysql_error()); 
  
  $result81 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'users VALUES
(1, "Anonymus", "", "Anonymus@Anonymus.com", NOW(), -1, 1, "0000-00-00 00:00:00", "", "", "", "", "", "", 0, 0, "", "", "", "", "", "", "", 0, 0, "","")') or die(mysql_error());
  
  
    if ($result8 and $result81) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>users</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}    
  
//TABLE word_filter
    $result9=mysql_query("CREATE TABLE $_SESSION[db_prefix]word_filter (
  words_id int(8) NOT NULL,
  bad_words mediumtext character set utf8 collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (words_id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;")or die(mysql_error()); 
  
  $result91 = mysql_query('INSERT INTO '.$_SESSION[db_prefix].'word_filter VALUES(0,"fuck, retard, idiot");') or die(mysql_error());
  
    if ($result9 and $result91) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>word_filter</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}    
  
 //TABLE post_reports
    $result10=mysql_query("CREATE TABLE $_SESSION[db_prefix]post_reports (
  report_id mediumint(8) NOT NULL auto_increment,
  post_id mediumint(8) NOT NULL,
  user_id mediumint(8) NOT NULL,
  user_name varchar(48) character set utf8 collate utf8_bin NOT NULL,
  user_ip varchar(40) character set utf8 collate utf8_bin NOT NULL,
  PRIMARY KEY  (report_id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;")or die(mysql_error());  
  
  if ($result10) {echo "<tr><td height='24px'><span style='font-size:12px;'>Table <b>post_reports</b> successfully created</span></td></tr>";}
  else {echo "<tr><td>An error occured! Please try again</td></tr>";}   
  
  echo '<tr><td><span style="font-size:12px;color:green;">Creating tables completed!
  <br><br><a class="menuButtons" href="install.php?step=3">Continue</a></span><br><br></td></tr>';
  
  echo "</table>";
  }
  
      if ($_GET['step'] ==3)
  {
  session_start();
  if ($_SERVER['REQUEST_METHOD'] != 'POST') {

  echo "<form method='post' action=''>
  
 <td align='left'><span style='font-size:12px;'><br>Step <b>$_GET[step] / 3</b><br><br>Setup forum and administrator:</span><br><br></td>
  </tr></table>";
  
  echo '<table border=1><tr><th >&nbsp;<b>Forum</b></th><th>&nbsp;</th></tr>
  <tr>
<td align="right" height="36"><span style="font-size:12px;"><b>Forum name</b></span></td><td align="left">&nbsp; <input value="brevisBB" size="50" type="text" name="forum_name" /></td></tr>

<tr>
<td align="right" height="36"><span style="font-size:12px;"><b>Forum description</b></span></td><td align="left">&nbsp; <input value="PHP simple forum" size="50" type="text" name="forum_desc" /></td></tr>

<tr><th >&nbsp;<b>Administrator</b></th><th>&nbsp;</th></tr>

<tr>
<td align="right" height="36"><span style="font-size:12px;"><b>Admin username</b></span></td><td align="left">&nbsp; <input value="admin" size="50" type="text" name="forum_admin" /></td></tr>

<tr>
<td align="right" height="36"><span style="font-size:12px;"><b>Admin password</b></span></td><td align="left">&nbsp; <input value="admin" size="50" type="password" name="forum_password" /></td></tr>

<tr>
<td align="right" height="36"><span style="font-size:12px;"><b>Admin email</b></span></td><td align="left">&nbsp; <input value="name@server.tld" size="50" type="text" name="forum_email" /></td></tr>

<td></td>
<td align="left" height="36">&nbsp; <input type="submit" class="inputButton" value="Submit" />
</table>';
}
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   if ($_POST['forum_admin']=="" OR $_POST['forum_password']=="")
   {echo "<table><tr><td height='36'><span style='font-size:12px;color:red'>Wrong or empty admin username or password</td></tr>
   <tr><td><a href='javascript: history.go(-1)'>Back to previous page</a></td></tr>
   </table>";die;}
   
  $server = get_value_of('$server'); $server = trim($server);
  $username = get_value_of('$username'); $username = trim($username);
  $password = get_value_of('$password'); $password = trim($password);
  $database = get_value_of('$database'); $database = trim($database);
  $table_prefix = get_value_of('$table_prefix'); $table_prefix = trim($table_prefix);
  
  $l_forumname = get_value_of('$l_forumname'); $l_forumname = trim($l_forumname); 
  $l_forumdesc = get_value_of('$l_forumdesc'); $l_forumdesc = trim($l_forumdesc); 
  
  
  $con = mysql_connect($_SESSION['server'],$_SESSION['db_username'],$_SESSION['db_password']);
  mysql_select_db($_SESSION['db_database'], $con);
  write_value_of('$server', "$server", $_SESSION['server']);
  write_value_of('$username', "$username", $_SESSION['db_username']);
  write_value_of('$password', "$password", $_SESSION['db_password']);
  write_value_of('$database', "$database", $_SESSION['db_database']);
  write_value_of('$table_prefix', "$table_prefix", $_SESSION['db_prefix']);
  
  write_value_of('$l_forumname', "$l_forumname", $_POST['forum_name']);
  write_value_of('$l_forumdesc', "$l_forumdesc", $_POST['forum_desc']);
  
  $admin = mysql_real_escape_string($_POST['forum_admin']); 
  $password = $_POST['forum_password'];
  $email = mysql_real_escape_string($_POST['forum_email']);
  
  $sql = "INSERT INTO
          " . $table_prefix . "users(user_name, user_pass, user_email ,user_date, user_level)
        VALUES('$admin',
             '" . sha1($password) . "',
             '$email',
            NOW(),
            1)";
	$result = mysql_query($sql) or die(mysql_error());		
    if (!$result) {
	 echo 'Something went wrong while registering. Please try again later.';}
	 
	 else {
	 //unset all variables
      $_SESSION['signed_in'] = null;
      $_SESSION['user_name'] = null;
      $_SESSION['user_id'] = null;
      $_SESSION['user_level'] = null;
      $_SESSION['ok'] = null;
      $_SESSION['user_lang'] = null;
	 echo "<table><tr><td height='36'><span style='font-size:12px;'><img src='../img/misc/thumbup.gif' align='absmiddle' />&nbsp; Forum successfully installed</span></td></tr>
	 <tr><td height='36'><span style='font-size:12px;'>Go to <a href='../index.php'><b>forum</b></a> or <a href='../adm/'><b>admin panel</b></a></span></td></tr>
	 <tr><td height='36'><span style='font-size:12px;color:red'>Please remove <b>install</b> folder</td></tr>
	 
	 </table>
	 ";
	 }
   }   
  }
  
 
echo "</table>";  
?>
<table><tr><td height="28"><div align="center">Powered by <a class="underline" href="http://brevisbb.iz.rs" target="_blank"><b>brevisBB</b></a> | Created by <a class="underline" href="http://dota.iz.rs" target="_blank"><b>Ivan A.</b></a> &copy; <?php print(date("Y")); ?>